// pages/index/search.js

const utils = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        search_key: '',
        search_hist: [],
        search_hot: []
    },
    jumpindex: function () {
        wx.switchTab({
            url: '../index/index'
        });
    },
    remove_hist: function () {
        var $this = this;
        wx.setStorage({
            key: 'search_hist',
            data: null,
        })
        $this.data.search_hist = [];
        $this.setData({
            "search_hist": $this.data.search_hist,
        });
    },
    search: function (val) {
        var $this = this;
        $this.data.search_key = val;
        $this.data.search_hist = $this.data.search_hist || [];
        if ($this.data.search_hist.length > 0) {
            var temp = false;
            for (var item in $this.data.search_hist) {
                if ($this.data.search_hist[item] == val) {
                    temp = true; //已经有了
                    break;
                }
            }
            if (!temp) {
                $this.data.search_hist.push(val);
            }
        } else {
            $this.data.search_hist.push(val);
        }


        $this.setData({
            "search_key": $this.data.search_key,
            "search_hist": $this.data.search_hist,
        });
        wx.setStorage({
            key: 'search_hist',
            data: $this.data.search_hist,
        });
        wx.navigateTo({
            url: '/pages/index/searchres?k=' + val,
        })
    },
    search_wc: function (e) {
        var $this = this;
        var val = e.detail.value;
        $this.search(val);
    },
    hot_click: function (e) {
        console.log(e);
        var $this = this;
        var val = e.target.dataset.text;
        $this.search(val);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // wx.setStorage({
        //   key: 'search_hist',
        //   data: null,
        // })
        wx.showLoading({
            title: '玩命加载中',
        });
        var $this = this;
        wx.getStorage({
            key: 'search_hist',
            success: function (res) {

                console.log('search history....')
                console.log(res);

                $this.data.search_hist = res.data;
            },
        });
        utils.geturl({
            "handler": "Home",
            "method":"getHotSearch"
        }, function (res) {
            if (res.code==200) {
                $this.data.search_hot = res.data.list;

                console.log(res)
                $this.setData({
                    "search_hot": $this.data.search_hot,
                    "search_hist": $this.data.search_hist,
                });

            }

        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})